import mysql from 'mysql2';

export const getDB = () : any => {

  try {

    // 创建一个数据库连接
    const connection = mysql.createConnection({
      host: 'localhost',
      user: 'root',
      database: 'jbook',
      password: '123456789',
      waitForConnections: false,
      connectionLimit: 10,
      queueLimit: 0
    });

    // 开启连接
    connection.connect(
      () => {
        console.log('连接成功');
      }
    );

    return {
      find: () => {
        return connection.promise();
      },
      close: () => {
        connection.end(
          () => {
            console.log('释放连接');
          }
        )
      }
    }
  }
  catch (error) {
    console.log('error', error);
  }
  
};